<?php

namespace App\Http\Middleware;

use App\backend\services\MenuServices;
use App\backend\services\RoleServices;
use App\Utils\JsonResp;
use Closure;
use Illuminate\Http\Request;
use app\backend\controllers\AdminBaseController;

class CheckToken extends AdminBaseController
{

    /**
     * 校验权限中间件
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse)  $next
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request, Closure $next)
    {
        // 校验路由判断是否正常
        $route = $request->path();
        if (!$route) {
            return JsonResp::error('获取路由失败');
        }

        // 校验token判断是否正常
        $token = $request->header('Authorization');
        if (!$token) {
            return JsonResp::error('获取信息失败');
        }

        // 校验用户是否登录状态正常
        /*$user_id = auth('backend')->id();
        if ($user_id == null) {
            return JsonResp::error('登录超时，请重新登录');
        }

        $role_user_data = RoleServices::getRoleUser($user_id);
        $role_user = object_to_array($role_user_data);

        // 用户是否是超级管理员
        if (!empty($role_user) && $role_user['id'] == 1) {
            return $next($request);
        }*/

        // 菜单路由鉴权
        /*$data = MenuServices::getRoleMenuUriList($role_user);
        if (!empty($data)) {
            if (!in_array($route, $data)) {
                return JsonResp::error('没有权限访问');
            }
        }*/

        return $next($request);
    }

}
